import {
  collectList,
  addCollect,
  delCollect,
  activityGoodsList,
  goodsList,
} from "@/common/apis/goods.js";

export default {
  state() {
    return {
      collectList: [],
    };
  },

  getters: {
    // 是否已收藏
    getIsCollect: (state) => (id) => {
      return state.collectList.some((i) => i.product.id == id);
    },
  },

  mutations: {
    delete(state, index) {
      state.list.splice(index, 1);
    },
  },

  actions: {
    async fetchGoodsList({ commit }, payload) {
      const response = await goodsList(payload);
      if (response) {
        return response;
      }
    },
    // 收藏列表
    async fetchCollectList({ state }, payload) {
      const response = await collectList(payload);
      if (response) {
        state.collectList = response.records;
      }
      return response;
    },
    // 添加收藏
    async fetchAddCollect({ state, dispatch }, id) {
      const response = await addCollect(id);
      if (response) {
        await dispatch("fetchCollectList");
      }
      return response;
    },
    // 取消收藏
    async fetchDelCollect({ state, dispatch }, id) {
      const response = await delCollect(id);
      if (response) {
        await dispatch("fetchCollectList");
      }
      return response;
    },
    async fetchActivityGoodsList({}) {
      const response = await activityGoodsList();
      if (response) {
        return response;
      }
    },
  },
};
